import { Injectable } from "@nestjs/common";
import { IsNotEmpty, IsOptional, Length } from "class-validator";
import { IsUnique } from "src/Constraint/unique.constraint";
import { UserEntity } from "../entity/user.entity";
import { IsUniqueExist } from "src/Constraint/unique.exist.constraint";

@Injectable()
export class UserCommonDto {
    @Length(4, 40, {
        message: '登录凭证 必须为 $constraint1到$constraint2 之间'
    })
    @IsNotEmpty()
    readonly credential!: string;
    @IsUnique(
        { entity: UserEntity },
        {

            message: '该用户名已被注册',
        },
    )
    @IsUniqueExist(
        { entity: UserEntity, ignore: 'id' },
        {

            message: '该用户名已被注册',
        },
    )
    @Length(4, 30, {
        always: true,
        message: '用户名长度必须为$constraint1到$constraint2',
    })
    @IsOptional()
    username!: string;
    @Length(3, 20, {
        always: true,
        message: '昵称必须为$constraint1到$constraint2',
    })
    @IsOptional({ always: true })
    nickname?: string;
    @IsUnique(
        { entity: UserEntity },
        {
            message: '手机号已被注册',

        },
    )

    @IsOptional()
    phone: string;
    @IsUnique(
        { entity: UserEntity },
        {
            message: '邮箱已被注册',

        },
    )

    @IsOptional()
    email: string;
   
    @Length(8, 50, {
        message: '密码长度不得少于$constraint1',
        always: true,
    })
    @IsOptional()
    readonly password!: string;

   // @IsMatch('password', { message: '两次输入密码不同', always: true })
    @IsNotEmpty({ message: '请再次输入密码以确认', always: true })
    readonly plainPassword!: string;
}


